import Engine from './Engine';
import { logSuccess, clearDir, createDir } from './utils';

interface Options {
  /** input path */
  input: string;
  /** output dir */
  output: string;
  /** is auto clear default is false  */
  autoClear?: boolean;
  /** output file name default is 'main.d.ts' */
  fileName?: string;
  /** which suffix file will generate default is ['.js', '.ts', '.tsx', '.jsx'] */
  extensions?: string[];
}

function generate(options: Options) {
  const engine = new Engine(process.cwd(), {
    extensions: options.extensions || ['.js', '.ts', '.tsx', '.jsx'],
  });

  const { input, output, fileName = 'main.d.ts' } = options;

  // 如果不存在 output 则创建
  createDir(output);
  // 清空 output 目录
  if (options.autoClear) {
    clearDir(output);
  }

  engine.generate(input, output, fileName);

  logSuccess('> Extract declaration success');
}

export { generate };
